package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;
import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> queryBrandByPage(Integer page, Integer rows,String sortBy,Boolean desc,String key) {
        //开启分页插件
        PageHelper.startPage(page, rows);

        //动态SQL的创建拼接工具
        Example example = new Example(Brand.class);

        //查询条件
        Example.Criteria criteria = example.createCriteria();

       if (StringUtils.isNoneBlank(key)){
         criteria.andLike("name","%" + key + "%");

       }

        if (StringUtils.isNotBlank(sortBy)){
            example.setOrderByClause(sortBy+(desc?" DESC":" ASC")); //给example条件设置排序方案，相当于sql语句中的order by
        }


        //使用通用Mapper查询
        List<Brand> brands = this.brandMapper.selectByExample(example);


        //判断是否查到条件
        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.BRAND_NO_FOUND);
        }

        //根据查询的情况统计符合当前查询的元素的总数，及总页数信息
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);

        //brands转换为BrandDTO
        List<BrandDTO> brandDTOS = BeanHelper.copyWithCollection(brands, BrandDTO.class);
        //封装分页信息，总的元素个数，总的页码数，当前页内的数据集合
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getPages(), brandDTOS);
    }

    @Transactional
    public void addBrand(BrandDTO brandDTO, List<Long> cids) {

        Brand brand=BeanHelper.copyProperties(brandDTO,Brand.class);
      //保存品牌信息，返回影响的行数
        int num = this.brandMapper.insertSelective(brand);
         if (num!=1){
             throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
         }
         //保存品牌和分类
              num = brandMapper.insertCategoryBrand(brand.getId(),cids);
                 if (num!=cids.size()){
                     throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
                 }
    }

       @Transactional
    public void updateBrand(BrandDTO brandDTO,List<Long> cids){
        //将brandDTO转换为Brand
        Brand brand = BeanHelper.copyProperties(brandDTO,Brand.class);
          //修改品牌
          int count= brandMapper.updateByPrimaryKeySelective(brand);
           if (count!= 1) {
               //更新失败，抛出异常
               throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
           }
           //删除中间表数据
           brandMapper.deleteCategoryBrand(brand.getId());
           //重新插入中间表数据
           count = brandMapper.insertCategoryBrand(brand.getId(),cids);
           if (count!=cids.size()){
               throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
           }
       }

       @Transactional(rollbackFor = LyException.class)
    public void deleteBrand(Long id) {
               //删除品牌信息
                this.brandMapper.deleteByPrimaryKey(id);
                //删除中间表
                this.brandMapper.deleteByBrandIdInCategoryBrand(id);

    }

    public BrandDTO queryBrandById(Long id) {
        Brand brand = this.brandMapper.selectByPrimaryKey(id);
        return BeanHelper.copyProperties(brand,BrandDTO.class);
    }
}
